package com.fenghen.service.impl;

import com.fenghen.config.RabbitMQConfig;
import com.fenghen.entity.ProductStock;
import com.fenghen.mapper.ProductStockMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MQConsumerService {

    @Autowired
    private ProductStockMapper productStockMapper;

    @RabbitListener(queues = RabbitMQConfig.STOCK_QUEUE)
    @Transactional
    public void receiveStockReduceMessage(Long produceId) {
        ProductStock productStock = productStockMapper.selectById(produceId);
        if (productStock != null && productStock.getStock() > 0) {
            productStock.setStock(productStock.getStock() - 1);
            productStock.setVersion(productStock.getVersion() + 1);
            productStockMapper.updateById(productStock);
        }
    }

}
